Skip to content

Fix: Support complete multihash type mappings in Prefix#76

Merged
seetadev merged 4 commits into
ipld:masterfrom
sumanjeet0012:fix-65-multihash-type-mappings
Jul 13, 2026
Merged

Fix: Support complete multihash type mappings in Prefix#76
seetadev merged 4 commits into
ipld:masterfrom
sumanjeet0012:fix-65-multihash-type-mappings

Conversation

@sumanjeet0012

@sumanjeet0012 sumanjeet0012 commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Closes #65

Description

This PR addresses the issue where Prefix._mh_types_to_code() and Prefix._mh_code_to_type() were using a hardcoded, incomplete mapping of only 8 multihash types, which led to ValueError on valid multihashes like identity, shake-256, and blake3.

The mapping logic now properly delegates to the multihash library which acts as the source of truth for all supported multiformat hash codes.

Changes

  • Replaced the hardcoded mappings in cid/prefix.py for both _mh_types_to_code and _mh_code_to_type.
  • _mh_type_to_code now uses multihash.Func and multihash.coerce_code() to look up the type name safely.
  • _mh_code_to_type now converts the code back to the canonical name via multihash.Func.

This enables py-cid to parse and serialize any valid prefix supported by python-multihash.

@sumanjeet0012 sumanjeet0012 force-pushed the fix-65-multihash-type-mappings branch from 4231e10 to 7d07d6c Compare July 12, 2026 18:53
@seetadev

Copy link
Copy Markdown
Contributor

Thanks for the excellent contribution, @sumanjeet0012!

This is another valuable improvement that simplifies the implementation while making py-cid more standards-compliant and future-proof. Replacing the hardcoded multihash mappings with the python-multihash APIs is the right design choice, as it removes duplicated logic and makes the library rely on the canonical source of truth for supported multihash codes and names.

This not only resolves the immediate issue with algorithms such as identity, shake-256, and blake3, but also ensures that newly supported multihash functions can be handled without requiring manual updates to py-cid. I also appreciate that both the forward and reverse mappings now consistently delegate to the multihash library, resulting in cleaner, more maintainable code with less room for inconsistencies over time.

With the linting fixes included and the full CI matrix passing successfully across all supported environments, this change looks solid. Overall, this improves interoperability across the multiformats ecosystem while reducing maintenance burden. LGTM, thanks for the thoughtful implementation and happy to merge.

CCing @acul71, @johannamoran, @mishmosh.

@seetadev seetadev merged commit baf1514 into ipld:master Jul 13, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prefix._mh_types_to_code() mapping is incomplete — only 8 hash types supported

2 participants